package com.ccnu.JobHub.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ccnu.JobHub.mapper.StudentMapper;
import com.ccnu.JobHub.model.database.Student;
import com.ccnu.JobHub.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentService extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    @Autowired
    private StudentMapper studentMapper;

    /**
     * 获取学生表内男生人数和女生人数以及未设置性别的人数
     * @return 一个Map类型对象
     */
    @Override
    public Map<String, Object> getMaleAndFemaleCount(){
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<Student>()
                .select("COUNT(IF(sex = 'M', 1, NULL)) AS male_count", "COUNT(IF(sex = 'F', 1, NULL)) AS female_count", "COUNT(IF(sex = 'U', 1, NULL)) AS unknown_count");
        List<Map<String ,Object>> maps = studentMapper.selectMaps(studentQueryWrapper);
        Map<String ,Object> result = new HashMap<>();
        result.put("MaleCount",maps.get(0).get("male_count"));
        result.put("FemaleCount",maps.get(0).get("female_count"));
        result.put("UnknownCount",maps.get(0).get("unknown_count"));

        return result;
    }

    @Override
    public Map<String, Object> getVerifiedCount(){
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<Student>()
                .select("COUNT(IF(authority = 1, 1, NULL)) AS verified_count", "COUNT(IF(authority = 0, 1, NULL)) AS unverified_count");
        List<Map<String ,Object>> maps = studentMapper.selectMaps(studentQueryWrapper);
        Map<String ,Object> result = new HashMap<>();
        result.put("VerifiedCount",maps.get(0).get("verified_count"));
        result.put("UnverifiedCount",maps.get(0).get("unverified_count"));

        return result;
    }

//    @Override
//    public List<Map<String ,Object>> getCountByRegister(String startDate, String now) {
//        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<Student>()
//                .select("Date(created_at) as date","count(*) as amount")
//                .between("created_at",startDate,now)
//                .groupBy("date");
//
//        return studentMapper.selectMaps(studentQueryWrapper);
//    }
}
